package com.qianfeng.openapi.sdk.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2021/1/6
 * @Time: 上午10:13
 */
public class HttpClientUtils {

    public static  HttpURLConnection getConnection(String url,String method,String enctype){
        try {
            //1.创建一个URL  相当于一个url连接
            URL myUrl = new URL(url);

            //2.得到一个HttpURLConnection
            HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection();

            //3.设置请求和响应参数
            connection.setDoInput(true);//允许接收服务端的值
            connection.setDoOutput(true);//允许向服务端传值  http://localhost:8005?method=xxx
            connection.setRequestMethod(method);//POST  GET  DELETE

            connection.setRequestProperty("Content-Type",enctype);
            // application/x-www-form-urlencoded   name=jack&password=admin    @RequestParam  或者 对象接参数
            //  application/json  @requestBody
            //  mutlipart
            return  connection;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return  null;

    }

    public static byte[]  builderQuery(Map<String,String> map){
        //method=aaa&sign=asasas&token=huihui
        StringBuffer sb = new StringBuffer();
        boolean f = false;
        for(Map.Entry<String,String> entry:map.entrySet()){

            String key = entry.getKey();
            String value = entry.getValue();
            if(f){
                sb.append("&");
            }else{
                f = true;
            }
            sb.append(key).append("=").append(value);

        }
        System.out.println("---->参数"+sb.toString());
        return  sb.toString().getBytes();

    }

    public static   String doGet(String url, Map<String,String> map){//http://localhost:8005?method=sjia   http://qf.openapi.com

        try {
            HttpURLConnection connection = getConnection(url,"GET","application/x-www-form-urlencoded");


            if (connection.getResponseCode()==200){

//                if (map!=null&&map.size()>0){
//                    connection.getOutputStream().write(builderQuery(map));
//                }
                return  getResult(connection.getInputStream());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        return  null;
    }

    /**
     *
     * @param url  http://localhost:8005
     * @param map
     * @return
     */
    public static   String doPost(String url, Map<String,String> map){//http://localhost:8005   http://qf.openapi.com

        try {
            HttpURLConnection connection = getConnection(url,"POST","application/x-www-form-urlencoded");
            if (map!=null&&map.size()>0){
                byte b[] = builderQuery(map);//method=jingdong.product.info&....

                connection.getOutputStream().write(b);//输出流
            }

            if (connection.getResponseCode()==200){

                return  getResult(connection.getInputStream());//服务端 --->程序
            }else{
                System.out.println(connection.getResponseCode()+"--"+connection.getInputStream());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        return  null;
    }

    public  static  String  getResult(InputStream inputStream){
        try {
            //InputStreamReader  转化流
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            StringBuffer sb = new StringBuffer();
            while((line=br.readLine())!=null){
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return  null;

    }





}
